{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itkwidgets import view\n",
    "from ipywidgets import interactive\n",
    "import ipywidgets as widgets\n",
    "import urllib.request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download image\n",
    "urllib.request.urlretrieve(\"http://raw.githubusercontent.com/InsightSoftwareConsortium/ITK/master/Examples/Data/VisibleWomanHeadSlice.png\", \"VisibleWomanHeadSlice.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read image\n",
    "image = itk.imread(\"VisibleWomanHeadSlice.png\")\n",
    "print(\"Image Type: %s\" % str(itk.template(image)))\n",
    "print(\"Image Pixel Type: %s\" % str(itk.template(itk.template(image)[1][0])))\n",
    "view(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Decompose image in 3 components: R, G, and B\n",
    "split_images = itk.split_components_image_filter(image)\n",
    "\n",
    "rgb_viewer = view(image, annotations=False)\n",
    "viewer = view(split_images[0], cmap=\"gray_Matlab\", annotations=False)\n",
    "\n",
    "# Define a function to use with ipywidgets `interactive`\n",
    "def view_channels(index=0):\n",
    "    # Update the image used in the viewer\n",
    "    viewer.image = split_images[index]\n",
    "slider = interactive(view_channels, index=(0, len(split_images)-1, 1))\n",
    "\n",
    "widgets.HBox([rgb_viewer, widgets.VBox([viewer, slider])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Blue Image Information: %s\" % str(split_images[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Recompose image with an Alpha layer.\n",
    "RGBAImageType = itk.Image[itk.RGBAPixel[itk.UC], image.GetImageDimension()]\n",
    "compose_image_filter = itk.ComposeImageFilter[split_images[0], RGBAImageType].New()\n",
    "compose_image_filter.SetInput(0, split_images[0])\n",
    "compose_image_filter.SetInput(1, split_images[1])\n",
    "compose_image_filter.SetInput(2, split_images[2])\n",
    "image_duplicator = itk.ImageDuplicator(split_images[2])\n",
    "image_duplicator.Update()\n",
    "alpha_layer = image_duplicator.GetOutput()\n",
    "alpha_layer.FillBuffer(255)\n",
    "compose_image_filter.SetInput(3, alpha_layer)\n",
    "\n",
    "compose_image_filter.Update()\n",
    "recomposed_image = compose_image_filter.GetOutput()\n",
    "print(recomposed_image)\n",
    "view(recomposed_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
